package com.itheima.redbaby.activity;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.R;
import com.itheima.redbaby.BaseActivity.MyHttpTask;
import com.itheima.redbaby.activity.AddressManageActivity.ViewHolder;
import com.itheima.redbaby.dao.domain.AddressDo;
import com.itheima.redbaby.engine.AddressEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class DiZhiGuanLiActivity extends BaseActivity implements OnClickListener {
	
	private TextView address_manager_add_text;
	private ListView address_manage_list;
	private AddressListAdapter adapter;
	private List<AddressDo> addressList;
	private TextView head_back_text;
	
	private Map<Integer,Object> mapValue;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume() {
		getData();
		super.onResume();
	}
	
	private void initView() {
		if(addressList==null||addressList.size()==0){ 
			setContentView(R.layout.address_empty_activity);
		}else{
			setContentView(R.layout.dizhi_manage_activity);
		
		address_manage_list = (ListView) findViewById(R.id.address_manage_list);
		adapter = new AddressListAdapter();
		address_manage_list.setAdapter(adapter);
		
		
		
		
		//点击事件 点击修改地址
		address_manage_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AddressDo address=addressList.get(position);
				Intent intent=new Intent();
				int addressId = address.getId();
				String areaDetail = address.getAreaDetail();
				int areaId = address.getAreaId();
				String phonenumber = address.getPhonenumber();
//				GloableParams.username;
//				intent.putExtra("address", address);
				intent.putExtra("addressId", addressId);
				intent.putExtra("areaDetail", areaDetail);
				intent.putExtra("areaId", areaId);
				intent.putExtra("phonenumber", phonenumber);
				
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
		
		} 
	
		
		//新增地址
		address_manager_add_text=(TextView) findViewById(R.id.address_manager_add_text);
		head_back_text = (TextView) findViewById(R.id.head_back_text);
		head_back_text.setOnClickListener(this);
		address_manager_add_text.setOnClickListener(this);
		
	}
	

	private void getData() {
		new MyHttpTask() {			
			
			@Override
			protected Object doInBackground(Void... params) {
				AddressEngine impl = DaoAndEngineFactory.getImpl(AddressEngine.class);
				//addressList = impl.getAddressList();
				
				Map<String, Object> mapAddress = impl.getMapAddress();
				if(mapAddress!=null)
				{
					addressList = (List<AddressDo>) mapAddress.get("addressList");
					mapValue = (Map<Integer, Object>) mapAddress.get("mapValue");
				}
				return null;
			}
			protected void onPostExecute(Object result) {
				initView();
				
			};
		}.executeHttp();
		
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch(v.getId()){
		case R.id.address_manager_add_text:
			intent=new Intent(DiZhiGuanLiActivity.this,AddAddressActivity.class);
			 startActivity(intent);
			break;
		case R.id.head_back_text:
			intent=new Intent(DiZhiGuanLiActivity.this,SummaryPaymentActivity.class);
			startActivity(intent);
			finish();
			break;
			
		default:
			break;
		}
	}
	
	private class AddressListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return addressList.size();
		}

		@Override
		public Object getItem(int position) {
			return addressList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ViewHolder holder;
			
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = View.inflate(DiZhiGuanLiActivity.this,
						R.layout.address_list_items, null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView
						.findViewById(R.id.addressnameTV);
				holder.phone = (TextView) convertView
						.findViewById(R.id.phoneTV);
				holder.address = (TextView) convertView
						.findViewById(R.id.address0TV);
				holder.addressDetial = (TextView) convertView
						.findViewById(R.id.addressdetilTV);
				convertView.setTag(holder);
			}
			AddressDo address = addressList.get(position);
			String result = (String) mapValue.get(position);
			holder.name.setText(address.getName());
			holder.phone.setText(address.getPhonenumber());
			holder.address.setText(result);
			holder.addressDetial.setText(address.getAreaDetail());
			return convertView;
		
			}

	}

	static class ViewHolder {
		TextView name;
		TextView phone;
		TextView address;
		TextView addressDetial;
	}
	
}
